package com.newly.common.base.exception.handler;

import cn.hutool.core.util.StrUtil;
import com.newly.common.base.constants.CodeConstant;
import com.newly.common.base.entity.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author gjm guan
 * @date 2021/10/21 21:12
 * @desc 参数异常处理
 */
@RestControllerAdvice
@Slf4j
public class ArgsExceptionHandle {

    @ExceptionHandler(IllegalArgumentException.class)
    public R illegalArgumentExceptionHandle(IllegalArgumentException e) {
        log.error("-----------------------------IllegalArgumentException begin-----------------------------");
        String msg = StrUtil.isBlank(e.getMessage()) ? "参数处理异常" : e.getMessage();
        R<Object> failed = R.fail(CodeConstant.ARGS_HANDLE_ERR, msg);
        log.error("error-> {}", failed);
        log.error("-----------------------------IllegalArgumentException after-----------------------------");
        return failed;
    }
}
